﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.Services.Protocols;
using OF.Security.Properties;

namespace OF.Security.Utils
{
    public static class ExceptionHelper
    {
        public static void TrueThrow(bool parseExpressionResult, string message, params object[] messageParams)
        {
            ExceptionHelper.TrueThrow<SystemSupportException>(parseExpressionResult, message, messageParams);
        }

        public static void TrueThrow<T>(bool parseExpressionResult, string message, params object[] messageParams) where T : Exception
        {
            Type typeFromHandle = typeof(T);
            if (!parseExpressionResult)
            {
                return;
            }
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }
            object obj = Activator.CreateInstance(typeFromHandle);
            ConstructorInfo constructor = typeFromHandle.GetConstructor(BindingFlags.Instance | BindingFlags.Public, null, CallingConventions.HasThis, new Type[]
            {
                typeof(string)
            }, null);
            constructor.Invoke(obj, new object[]
            {
                string.Format(message, messageParams)
            });
            throw (Exception)obj;
        }

        public static void FalseThrow(bool parseExpressionResult, string message, params object[] messageParams)
        {
            ExceptionHelper.TrueThrow(!parseExpressionResult, message, messageParams);
        }

        public static void FalseThrow<T>(bool parseExpressionResult, string message, params object[] messageParams) where T : Exception
        {
            ExceptionHelper.TrueThrow<T>(!parseExpressionResult, message, messageParams);
        }

        public static void CheckStringIsNullOrEmpty(string data, string paramName)
        {
            if (string.IsNullOrEmpty(data))
            {
                throw new ArgumentException(string.Format(Resources.StringParamCanNotBeNullOrEmpty, paramName));
            }
        }

        public static Exception GetRealException(Exception ex)
        {
            Exception result = ex;
            if (ex is SoapException)
            {
                result = new SystemSupportException(ExceptionHelper.GetSoapExceptionMessage(ex), ex);
            }
            else
            {
                while (ex != null && (ex is HttpUnhandledException || ex is HttpException || ex is TargetInvocationException))
                {
                    if (ex.InnerException != null)
                    {
                        result = ex.InnerException;
                    }
                    else
                    {
                        result = ex;
                    }
                    ex = ex.InnerException;
                }
            }
            return result;
        }

        public static string GetSoapExceptionMessage(Exception ex)
        {
            string text = ex.Message;
            if (ex is SoapException)
            {
                int num = text.LastIndexOf("--> ");
                if (num > 0)
                {
                    text = text.Substring(num + 4);
                    num = text.IndexOf(": ");
                    if (num > 0)
                    {
                        text = text.Substring(num + 2);
                        num = text.IndexOf("\n   ");
                        text = text.Substring(0, num);
                    }
                }
            }
            return text;
        }
    }
}
